package mapspinnertests;

import jmetest.effects.water.TestQuadWater;

import com.jme.image.Texture;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

public class SkyBoxFactory {
	public static Skybox buildSkyBox(final DisplaySystem display) {
		final Skybox skybox = new Skybox("skybox", 2, 2, 2);

		final String dir = "jmetest/data/skybox1/";
		final Texture north = TextureManager.loadTexture(TestQuadWater.class
				.getClassLoader().getResource(dir + "1.jpg"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear);
		final Texture south = TextureManager.loadTexture(TestQuadWater.class
				.getClassLoader().getResource(dir + "3.jpg"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear);
		final Texture east = TextureManager.loadTexture(TestQuadWater.class
				.getClassLoader().getResource(dir + "2.jpg"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear);
		final Texture west = TextureManager.loadTexture(TestQuadWater.class
				.getClassLoader().getResource(dir + "4.jpg"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear);
		final Texture up = TextureManager.loadTexture(TestQuadWater.class
				.getClassLoader().getResource(dir + "6.jpg"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear);
		final Texture down = TextureManager.loadTexture(TestQuadWater.class
				.getClassLoader().getResource(dir + "5.jpg"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear);

		skybox.setTexture(Skybox.Face.North, north);
		skybox.setTexture(Skybox.Face.West, west);
		skybox.setTexture(Skybox.Face.South, south);
		skybox.setTexture(Skybox.Face.East, east);
		skybox.setTexture(Skybox.Face.Up, up);
		skybox.setTexture(Skybox.Face.Down, down);
		skybox.preloadTextures();

		final CullState cullState = display.getRenderer().createCullState();
		cullState.setCullFace(CullState.Face.None);
		cullState.setEnabled(true);
		skybox.setRenderState(cullState);

		final ZBufferState zState = display.getRenderer().createZBufferState();
		zState.setEnabled(false);
		skybox.setRenderState(zState);

		final FogState fs = display.getRenderer().createFogState();
		fs.setEnabled(false);
		skybox.setRenderState(fs);

		skybox.setLightCombineMode(Spatial.LightCombineMode.Off);
		skybox.setCullHint(Spatial.CullHint.Never);
		skybox.setTextureCombineMode(TextureCombineMode.Replace);
		skybox.updateRenderState();

		skybox.lockBounds();
		skybox.lockMeshes();
		return skybox;
	}

}
